/*
 * @Author: wangcs
 * @Date: 2021-03-29 11:40:49
 * @LastEditTime: 2021-09-06 10:44:42
 * @LastEditors: wangcs
 * @Description: 服务入口
 * @FilePath: \shanheweb\server\server.ts
 */

import { lt } from "semver";
// check runtime
if (lt(process.version, "20.0.0")) {
  console.error("Node Version should be greater than 20.0.0");
  process.exit(-1);
}
const Koa = require("koa"); // 不使用import的原因是Koa.prototype.apply会报错

import bodyParser from "koa-bodyparser";
import { router } from "./routers/index";
import { GlobVar } from "./global";
import boot from "./components/boot";
import locale from "./components/locale";
import logging from "./components/logging";
import errorProcess from "./components/errorProcess";

Koa.prototype.apply = function (module: any, ...rest: any) {
  module(this, ...rest);
  return this;
};

const app = new Koa();

// app.use(bodyParser());

app.keys = ["sd->_<"];
app
  .apply(boot)
  .apply(locale)
  .apply(logging)
  .apply(errorProcess)
  .use(router.routes())
  .use(router.allowedMethods());

app.listen(GlobVar.PORT, () => {
  console.warn(`sk app running at port ${GlobVar.PORT}`);
});
